package com.billdowney.qq.robot.controller;

import com.alibaba.fastjson.JSON;
import com.billdowney.qq.robot.bean.Dayz.Dayzurl;
import com.billdowney.qq.robot.bean.Dayz.Dayzuser;
import com.billdowney.qq.robot.bean.QQjiqiren.Liaotianjilu;
import com.billdowney.qq.robot.bean.QQjiqiren.Qunxinxi;
import com.billdowney.qq.robot.bean.QQjiqiren.Shijianzhiling;
import com.billdowney.qq.robot.bean.QQjiqiren.tuisongxiaoxi;
import com.billdowney.qq.robot.service.Dayz.IDayzuserService;
import com.billdowney.qq.robot.service.QQjiqiren.IDayzurlService;
import com.billdowney.qq.robot.service.QQjiqiren.ILiaotianjiluService;
import com.billdowney.qq.robot.service.QQjiqiren.IQunxinxiService;
import com.billdowney.qq.robot.service.QQjiqiren.IShijianzhilingService;
import com.billdowney.qq.robot.util.IpUtil;
import com.billdowney.qq.robot.util.ReadLocalJsonFileDemo;
import com.billdowney.qq.robot.util.ShiJianUtil;
import io.swagger.annotations.Api;
import net.mamoe.mirai.Bot;
import net.mamoe.mirai.contact.Group;
import net.mamoe.mirai.event.events.GroupMessageEvent;
import net.mamoe.mirai.message.data.At;
import net.mamoe.mirai.message.data.Image;
import net.mamoe.mirai.message.data.MessageChain;
import net.mamoe.mirai.message.data.SingleMessage;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.IOException;
import java.net.SocketException;
import java.util.*;

import static org.apache.commons.lang.StringUtils.substringBefore;

@Api(tags=" qq机器人接口",value=" qq机器人接口")
@Component
@RestController
@RequestMapping("kan")

public class qqjiqirenController {
    @Autowired
    private IDayzuserService dayzuserService;
    @Autowired
    private IQunxinxiService qunxinxiService;
    @Autowired
    private IDayzurlService dayzurlService;
    @Autowired
    private ILiaotianjiluService liaotianjiluService;
    @Autowired
    private IShijianzhilingService shijianzhilingService;

    private List<Dayzuser> 所有用户 = new ArrayList<>();  //群用户

    private List<Qunxinxi> 所有群信息 =new ArrayList<>();//本服务器所有
    private void 初始化数组() throws SocketException {
        if (所有群信息.size()!=0)return;
        Qunxinxi qunxinxi =new Qunxinxi();
        qunxinxi.setIp(IpUtil.getLocalIp4Address().get().toString().replaceAll("/",""));
        this.所有群信息 = qunxinxiService.selectQunxinxiList(qunxinxi);//缓存当前服务器的群信息
    }
    @PostMapping("s1")
    public void s1s(GroupMessageEvent event) throws IllegalAccessException, IOException {
        初始化数组();//初始化一次






        MessageChain message = event.getMessage();
        SingleMessage singleMessage = message.get(1);
        tuisongxiaoxi tu = new tuisongxiaoxi();
        tu.setQqname(event.getSenderName());/**/
//        tu.setQqname(removeFourChar(event.getSenderName()));
        tu.setMQ_fromID(event.getGroup().getId()+""); //
        tu.setMQ_robot(event.getBot().getId()+"");//
        tu.setMQ_fromQQ(event.getSender().getId()+"");
        tu.setMQ_msg(singleMessage.toString());            //获取发言内容
//        tu.setMQ_msg(removeFourChar(singleMessage.toString()));            //获取发言内容//过滤特殊字符
        if (message.get(Image.Key)!=null) {
            tu.setWenjian(message.get(Image.Key).getImageId());  //获取消息中的图片等参
        }
        tu.setMQ_msgID(event.getGroup().getId()+"");
        tu.setMQ_type(0);
        获取当前用户信息(tu);//用户发言时缓存用户信息
        tuisongxiaoxi 重定义回复缓存= new tuisongxiaoxi();
        重定义回复缓存.setMQ_fromQQ(tu.getMQ_fromQQ());
        重定义回复缓存.setMQ_msgID(tu.getMQ_msgID());
        重定义回复缓存.setMQ_robot(tu.getMQ_robot());


//        操作群权限();

//        {
//            //聊天记录生成文件保存
////        String fileName = "D:\\WWWROOT\\zcgl_a14syl\\web\\Pinche\\消息文件.txt";
////        appendMethod_two(fileName, "\n"+JSON.toJSON(tu).toString());
////        if (1==1) return;
//        }

        String 内容="" ;
        用户发言进行记录(tu);//用户发言进行记录
        int 内容长度 = 内容.length();
        if (tu.getMQ_msg().equals("我是群主")) {
            内容+= 绑定群主(tu);
        } else  if (tu.getMQ_msg().startsWith("购买")){
            内容 += 购买(tu);
        } else  if (tu.getMQ_msg().equals("开启机器人")) {
            内容 += 开机(tu);
        } else if (群状态(tu)) {
            return;//群机器人关机状态不做回复
        }else if (tu.getMQ_msg().equals("关闭机器人")) {
            内容 += 关机(tu);
        }

        if (!判断群时间(tu)) {//群机器人使用时间不足
            内容 += "\n 警告：群机器人使用时间不足请联系群主充值" +
                    "\n 命令：关闭机器人，可关闭" +
                    "\n 命令：开启机器人，可开启";
        } else if (tu.getMQ_msg().startsWith("绑定") && tu.getMQ_msg().length() > 18 && tu.getMQ_msg().length() < 22) {
            内容 += 绑定(tu);
        } else if (tu.getMQ_msg().contains("url")) {
            内容 +=添加文件路径(tu);
        } else if (tu.getMQ_msg().equals("个人信息")) {
            内容 += 查看个人信息(tu);
        } else if (tu.getMQ_msg().startsWith("存入")) {
            内容 += 存入(tu);
        } else if (tu.getMQ_msg().startsWith("取出存款")) {
            内容 += 取出存款(tu);
        } else if (tu.getMQ_msg().startsWith("取出利息")) {
            内容 += 取出利息(tu);
        } else if (tu.getMQ_msg().startsWith("转账")){
            内容+=转账(tu);
        } else if (tu.getMQ_msg().equals("更新机器人")) {
                更新机器人(tu);
        } else if (tu.getMQ_msg().equals("我的存款")) {
            内容 += 我的存款(tu);
        } else if (tu.getMQ_msg().equals("签到")) {
            内容 += 签到(tu);
        } else if (tu.getMQ_msg().startsWith("添加用户金额")) {
            内容 += 添加用户金额(tu);
        } else if (tu.getMQ_msg().startsWith("添加全体金额")) {
            内容 += 添加全体金额(tu);
        } else if (tu.getMQ_msg().startsWith("添加用户vip等级")) {
            内容 += 添加用户vip等级(tu);
        } else if (tu.getMQ_msg().startsWith("每日收益")) {
            内容 += 每日收益(tu);
        } else if (tu.getMQ_msg().startsWith("服务器ip")) {
            内容 += 服务器ip(tu);
        } else if (tu.getMQ_msg().equals("查看用户信息")) {
            内容 += 查看用户信息(tu);
        } else if (tu.getMQ_msg().startsWith("添加欢迎语句")) {
            内容 += 添加欢迎语句(tu);
        } else if (tu.getMQ_msg().startsWith("清理缓存")) {
            内容 += 清理缓存(tu);
        } else if (tu.getMQ_msg().startsWith("记录事件")) {
            内容 += 记录事件(tu);
        } else if (tu.getMQ_msg().startsWith("当前版本")) {
            内容 += "当前版本等级 0.0.0.3";
        } else if (tu.getMQ_msg().startsWith("修改事件")) {
            内容 += 修改事件(tu);
        } else if (tu.getMQ_msg().startsWith("删除事件")) {
            内容 += 删除事件(tu);
        }

        内容 += 触发事件(tu);
        if (内容.length() != 内容长度) {
            发送群消息(重定义回复缓存, 内容);
        }

    }

    private void 操作群权限() {
        Map<String,String> map = new HashMap<>();
        map.put("测试1","1");
        map.put("测试2","1");
        map.put("测试3","1");
        map.put("测试4","1");
        map.put("测试5","1");
        map.put("测试6","1");
        map.put("测试7","1");
        Qunxinxi qinxinxi = new Qunxinxi();
        qinxinxi.setId(18L);
        qinxinxi.setQunqq("546729209");
        qinxinxi.setGongnengquanxian(map.toString());
        qunxinxiService.updateQunxinxi(qinxinxi);
    }


    private String 清理缓存(tuisongxiaoxi tu) {
        if (!群主判定(tu)){
            return "不是最高权限，请群主或开发者执行此命令";
        }
        所有用户=new ArrayList<>();
        所有群信息 =new ArrayList<>();
        return "缓存清理完毕,机器人已进入重启完毕状态";
    }


    private void 更新机器人(tuisongxiaoxi tu) throws IOException {
        if (!群主判定(tu)){
            return;
        }
        发送群消息(tu, "机器人正在下载更新文件");
        FileUpload f= new FileUpload();
        f.机器人下载包();
        f.脚本下载();
        发送群消息(tu, "机器人正在重启预计重启时间30秒");
        Runtime.getRuntime().exec("cmd /E:ON /c start C:/qq机器人服务器部署包/启动qq机器人.bat");
    }

    private String 添加用户vip等级(tuisongxiaoxi tu) {
        if (!群主判定(tu)) {//不是本群群主
            return " 用户权限不足";//用户权限不足
        }
        String userqq = tu.getMQ_msg().substring(9, tu.getMQ_msg().indexOf(","));
        int dengji = Integer.parseInt(tu.getMQ_msg().substring(tu.getMQ_msg().indexOf(",") + 1));
        Dayzuser dayzuser =获取当前用户信息(tu);
        dayzuser.setVip(dengji);
        修改缓存用户(dayzuser);
        return    " 用户 qq:" + userqq +
                "\n vip等级:" + dengji;
    }


    private void 发送群消息(tuisongxiaoxi tu,String neirong) {
        System.out.println(neirong+":"+"消息回复");
        Bot b= Bot.getInstance(Long.parseLong(tu.getMQ_robot())); //qq机器人
        Group group = b.getGroup(Long.parseLong(tu.getMQ_msgID()));//目标群
//        Friend friend = b.getFriend(Long.parseLong(tu.getMQ_fromQQ()));//目标好友
//        friend.sendMessage("好友消息回复");
        At at= new At(Long.valueOf(tu.getMQ_fromQQ()));



        if (neirong.contains("}.")) {//回复内容有图片
            Image.Builder image = Image.newBuilder(neirong);
            group.sendMessage(at.plus(image.build()));
        }else {
            group.sendMessage(at.plus("\n"+neirong));
        }
    }

    private String 转账(tuisongxiaoxi tu) {
        String zhuangxiangqq =  tu.getMQ_msg().substring(2,tu.getMQ_msg().indexOf(","));//获得转账目标的qq
        int zhuanjiner = Integer.parseInt(tu.getMQ_msg().substring(tu.getMQ_msg().indexOf(",")+1));
        Dayzuser dayzuser =获取当前用户信息(tu);
        int jiner= Integer.parseInt(dayzuser.getJiner());
        int shengyu =  jiner-zhuanjiner;
        dayzuser.setJiner(shengyu+"");
        if (shengyu < 0) {
            return " 用户金额不足,请检查银行金额";
        }
        tu.setMQ_fromQQ(zhuangxiangqq);
        Dayzuser dayzuser1 =  获取当前用户信息(tu);
           if (dayzuser1==null){
               return " 目标不存在,请对方发言";
           }
        String 目标用户转账之前金额 = dayzuser1.getJiner();
        int mubiao = Integer.parseInt( dayzuser1.getJiner())  + zhuanjiner;
        dayzuser1.setJiner(mubiao+"") ;

        修改缓存用户(dayzuser1);
        修改缓存用户(dayzuser);

        return  "机器人银行存款:" + jiner +
                "\n机器人银行存款:-" + zhuanjiner +
//                "\n用户名称:" + dayzuser.getName() +
                "\n机器人银行存款:" + dayzuser.getJiner()+
                "\n目标名称:"+dayzuser1.getName()+
                "\n目标机器人银行:"+目标用户转账之前金额+
                "\n目标机器人银行:+"+zhuanjiner+
                "\n目标机器人银行:"+dayzuser1.getJiner();


    }

    private String 购买(tuisongxiaoxi tu) {
        if (!tu.getMQ_robot().equals("1011450786")){
            return "请添加qq：1011450786私聊 进行充值 或者 添加2570889556 开发者";
        }
        String chufa = tu.getMQ_msg().substring( 2,tu.getMQ_msg().indexOf(","));
        String qunid = tu.getMQ_msg().substring(tu.getMQ_msg().indexOf(",")+1);
        Shijianzhiling shijian = new Shijianzhiling();
        shijian.setChufa(chufa);
         List<Shijianzhiling>  list =   shijianzhilingService.selectdingshirenwuList(shijian);
         if (list.size()==0){
             return "商品不存在";
         }
        JSONObject json = JSONObject.fromObject(list.get(0).getXiangying()); //商品信息
        Qunxinxi qunxinxi = new Qunxinxi();
        qunxinxi.setQunqq(qunid);
        List<Qunxinxi> qxx =  qunxinxiService.selectQunxinxiList(qunxinxi);
        if (qxx.size()==0){
            return "群没有绑定机器人 或 群号输入错误";
        }
        String daoqishijian= qxx.get(0).getDaoqishijian();
        Dayzuser dayzuser = new Dayzuser();
        dayzuser.setUserqqqun(tu.getMQ_fromID());
        dayzuser.setDazyuserqq(tu.getMQ_fromQQ());
        List<Dayzuser> dayzusers=  dayzuserService.selectDayzuserList(dayzuser);//获取用户信息
        double cunru = Double.parseDouble(json.get("jiner")+"");     //修改指令需要的钱
        double xianyou = dayzusers.get(0).getZhuanzhang();//数据库中的金额
        double shengyu = xianyou - cunru;
        if (shengyu < 0) {
            return " R M B不足无法充值 \n 当前R M B"+xianyou;//金额不足
        }
        dayzusers.get(0).setZhuanzhang(shengyu);
        修改缓存用户(dayzusers.get(0));
           if (ShiJianUtil.shijianduibi(daoqishijian,ShiJianUtil.shijian(new Date()))){  //如果群已经到期
               qxx.get(0).setDaoqishijian(  ShiJianUtil.monthAddFrist(ShiJianUtil.shijian(new Date()), Integer.parseInt(json.get("name")+""))); //从现在添加时间
           }else {
               qxx.get(0).setDaoqishijian(   ShiJianUtil.monthAddFrist(daoqishijian, Integer.parseInt(json.get("name")+""))); //从到期时间添加时间
           }
        qunxinxiService.updateQunxinxi(qxx.get(0));
        return "qq群充值成功," +
                "\n 用户剩余R M B :"+shengyu+"元" +
                "\n 群到期时间     :"+qxx.get(0).getDaoqishijian();
    }
    private String 删除事件(tuisongxiaoxi tu) {//删除事件
        String chufa = tu.getMQ_msg().substring(4);
        Shijianzhiling shijianzhiling = new Shijianzhiling();
        shijianzhiling.setQqqun(tu.getMQ_fromID());
        shijianzhiling.setChufa(chufa);
        shijianzhiling.setJiluqq(tu.getMQ_fromQQ());
        if (chufa.contains("商品")&&tu.getMQ_fromQQ().equals("2570889556")){
            return " 你不是开发人员无法删除商品";
        }else {
            shijianzhiling.setQqqun(null);
            shijianzhiling.setJiluqq(null);
        }
        if (chufa.contains("vip等级")&&!群主判定(tu)){
            return " 你不是最高权限无法删除vip";
        }
        List<Shijianzhiling> list = shijianzhilingService.selectShijianzhilingList(shijianzhiling);
        if (list.size() == 0) {
            return " 事件不存在，删除失败";
        }
        shijianzhilingService.deleteShijianzhilingById(list.get(0).getChufa());
        if (群主判定(tu)) {
            return " 最高权限事件删除成功";
        }
        Dayzuser dayzuser = 获取当前用户信息(tu);
        int cunru = 100000;     //修改指令需要的钱
        int xianyou = Integer.parseInt(dayzuser.getJiner());//数据库中的金额
        int shengyu = xianyou - cunru;
        if (shengyu < 0) {
            return " 金额不足10w无法删除";//金额不足
        }
        dayzuser.setJiner(shengyu + "");
        修改缓存用户(dayzuser);
        return " 事件删除成功" +
                "\n 银行存款:-" + cunru +
                "\n 用户名称：" + dayzuser.getName() +
                "\n 银行存款：" + dayzuser.getJiner();
    }
    private String 触发事件(tuisongxiaoxi tu) {
        if (tu.getMQ_msg().length()==0){
            return "";
        }
        Shijianzhiling shijianzhiling = new Shijianzhiling();
        if (tu.getMQ_msg().length() >= 5 ) {
            shijianzhiling.setQqqun(tu.getMQ_fromID());
        }
        shijianzhiling.setChufa(tu.getMQ_msg());

        List<Shijianzhiling> list = shijianzhilingService.selectShijianzhilingList(shijianzhiling);
        if (list.size() == 0) {
            return "";
        }
        return list.get(0).getXiangying();
    }

    private String 修改事件(tuisongxiaoxi tu) {//修改事
        String chufa = tu.getMQ_msg().substring(4, tu.getMQ_msg().indexOf(","));
        String xiangying = tu.getMQ_msg().substring(tu.getMQ_msg().indexOf(",") + 1);
        Shijianzhiling shijianzhiling = new Shijianzhiling();
        shijianzhiling.setQqqun(tu.getMQ_fromID());
        shijianzhiling.setChufa(chufa);
        shijianzhiling.setJiluqq(tu.getMQ_fromQQ());
        if (chufa.contains("商品")&&tu.getMQ_fromQQ().equals("2570889556")){
            return " 你不是开发人员无法修改商品";
        }else {
            shijianzhiling.setQqqun(null);
            shijianzhiling.setJiluqq(null);
        }
        if (chufa.contains("vip等级")&&!群主判定(tu)){
            return " 你不是最高权限无法修改vip";
        }
        List<Shijianzhiling> list = shijianzhilingService.selectShijianzhilingList(shijianzhiling);
        if (list.size() == 0) {
            return " 事件不存在，请改为记录事件";
        }
        list.get(0).setJilvjiqirenqq(tu.getMQ_robot());
        list.get(0).setXiangying(xiangying);
        shijianzhilingService.updateShijianzhiling(list.get(0));
        if (群主判定(tu)) {
            return " 最高权限事件修改成功";
        }
        Dayzuser dayzuser = 获取当前用户信息(tu);
        int cunru = 100000;     //修改指令需要的钱
        int xianyou = Integer.parseInt(dayzuser.getJiner());//数据库中的金额
        int shengyu = xianyou - cunru;
        if (shengyu < 0) {
            return " 金额不足10w无法修改";//金额不足
        }
        dayzuser.setJiner(shengyu + "");
        修改缓存用户(dayzuser);
        return " 事件修改成功" +
                "\n 银行存款:-" + cunru +
                "\n 用户名称：" + dayzuser.getName() +
                "\n 银行存款：" + dayzuser.getJiner();
    }
    private String 记录事件(tuisongxiaoxi tu) {
        String chufa = tu.getMQ_msg().substring(4, tu.getMQ_msg().indexOf(","));
        if (chufa.contains("商品")&&tu.getMQ_fromQQ().equals("2570889556")){
            return " 发你不是开人员无法添加商品";
        }
        if (chufa.contains("vip等级")&&!群主判定(tu)){
            return " 你不是最高权限无法添加vip";
        }
        String xiangying = tu.getMQ_msg().substring(tu.getMQ_msg().indexOf(",") + 1);
        Shijianzhiling shijianzhiling = new Shijianzhiling();
        shijianzhiling.setQqqun(tu.getMQ_fromID());
        shijianzhiling.setChufa(chufa);
        shijianzhiling.setJiluqq(tu.getMQ_fromQQ());
        List<Shijianzhiling> list = shijianzhilingService.selectShijianzhilingList(shijianzhiling);
        if (list.size() != 0) {
            return " 事件存在，请改为修改事件";
        }
        shijianzhiling.setJilvjiqirenqq(tu.getMQ_robot());
        if (tu.getWenjian()!=null){
            shijianzhiling.setXiangying(xiangying+tu.getWenjian());
        }else {
            shijianzhiling.setXiangying(xiangying);
        }
        shijianzhilingService.insertShijianzhiling(shijianzhiling);
        if (群主判定(tu)) {
            return " 最高权限事件记录成功";
        }
        Dayzuser dayzuser = 获取当前用户信息(tu);
        int cunru = 100000;     //修改指令需要的钱
        int xianyou = Integer.parseInt(dayzuser.getJiner());//数据库中的金额
        int shengyu = xianyou - cunru;
        if (shengyu < 0) {
            return " 金额不足10w无法记录";//金额不足
        }
        dayzuser.setJiner(shengyu + "");
        修改缓存用户(dayzuser);
        return " 事件记录成功" +
                "\n 银行存款: -" + cunru +
                "\n 用户名称:" + dayzuser.getName() +
                "\n 银行存款:" + dayzuser.getJiner();

    }
    private String 添加欢迎语句(tuisongxiaoxi tu) {
        if (!群主判定(tu)) {
            return " 添加失败，权限不足";
        }
        String huanying = tu.getMQ_msg().substring(6);
        Qunxinxi qunzhu = new Qunxinxi();
        qunzhu.setQunqq(tu.getMQ_fromID());
        qunzhu.setQunzhu(tu.getMQ_fromQQ());
        List<Qunxinxi> list = qunxinxiService.selectQunxinxiList(qunzhu);
        list.get(0).setHuanyingyuju(huanying);
        qunxinxiService.updateQunxinxi(list.get(0));
        return " 群欢迎语句：添加/修改成功";
    }
    public void 进群提示语(tuisongxiaoxi tu) {
        String neirong = " \n";
        Qunxinxi qunzhu = new Qunxinxi();
        qunzhu.setQunqq(tu.getMQ_fromID());
        List<Qunxinxi> list = qunxinxiService.selectQunxinxiList(qunzhu);
        if (list.size() == 0 || list.get(0).getHuanyingyuju() == null) {
            neirong += " 欢迎新人入群";
        } else {
            neirong += list.get(0).getHuanyingyuju();
        }
        发送群消息(tu,neirong);
    }
    boolean 群状态(tuisongxiaoxi tu) {
        Qunxinxi qunxinxi = new Qunxinxi();
        qunxinxi.setQunqq(tu.getMQ_fromID());
        List<Qunxinxi> list = qunxinxiService.selectQunxinxiList(qunxinxi);
        if (list.size() == 0) {
            return true;
        } else {
            if (list.get(0).getKaiguanji() == 1) {
                return true;
            }
        }
        return false;
    }
    String 关机(tuisongxiaoxi tu) {
        Qunxinxi qunxinxi = new Qunxinxi();
        qunxinxi.setQunqq(tu.getMQ_fromID());
        List<Qunxinxi> list = qunxinxiService.selectQunxinxiList(qunxinxi);
        if (list.size() == 0) {
            return " 请先输入命令：我是群主";
        }
        if (!群主判定(tu)) {
            return "关机失败，权限不足";
        }
        list.get(0).setKaiguanji(1);
        qunxinxiService.updateQunxinxi(list.get(0));
        return " 机器人已关闭";
    }
    String 开机(tuisongxiaoxi tu) {
        Qunxinxi qunxinxi = new Qunxinxi();
        qunxinxi.setQunqq(tu.getMQ_fromID());
        List<Qunxinxi> list = qunxinxiService.selectQunxinxiList(qunxinxi);
        if (list.size() == 0) {
            return " 请先输入命令：我是群主";
        }
        if (!群主判定(tu)) {
            return "开机失败，权限不足";
        }
        list.get(0).setKaiguanji(0);
        qunxinxiService.updateQunxinxi(list.get(0));
        return " 机器人已开机";
    }
    boolean 判断群时间(tuisongxiaoxi tu) {
        Qunxinxi qunxinxi = new Qunxinxi();
        qunxinxi.setQunqq(tu.getMQ_fromID());
        List<Qunxinxi> list = qunxinxiService.selectQunxinxiList(qunxinxi);
        if (list.size() == 0) {
            return false;
        }
        return ShiJianUtil.shijianduibi(list.get(0).getDaoqishijian());
    }

    public String 查看用户信息(tuisongxiaoxi tu) {
        int ss = 0;
        String map ="";
        if (群主判定(tu)){
            Dayzuser user = new Dayzuser();
            user.setUserqqqun(tu.getMQ_fromID());
            List<Dayzuser> list =  dayzuserService.selectDayzuserList(user);
            if(list.size()==0){
                return " 用户数量:0";
            }
            for (Dayzuser user1:list){
                Dayzurl dayzurl = new Dayzurl();
                dayzurl.setQunqq(tu.getMQ_fromID());
                dayzurl.setName("银行");
                List<Dayzurl> dayzurls = dayzurlService.selectDayzurlList(dayzurl);//获取银行url
                if (dayzurls.size() == 0) {
                    return " 银行地址未设定请联系群主";
                }
                String s = dayzurls.get(0).getUrl() + "/" + user1.getSteamid() + ".json";
                File file = new File(s);
                Map<String, Object> map1 = new HashMap<>();
                try {
                    map1 = JSON.parseObject(ReadLocalJsonFileDemo.readerMethod(file));//获取文件对象
                } catch (Exception e) {
//                    return "Please touch ATM";//请触碰ATM
                }
                ss++;

                String m_username =null;
                m_username= (String) map1.get("m_Username");
                if (m_username==null||m_username=="") {
                    m_username = (String) map1.get("m_PlayerName");
                }

                map+= " ID:"+ ss +
                        "\n 名称:" + m_username+
                        "\n QQ号:"+user1.getDazyuserqq() +
                        "\n 卢布:" + map1.get("m_OwnedCurrency") +
                        "\n 银行存款:"+user1.getJiner()+
                        "\n 利息:"+user1.getLixi()+
                        "\n-------------------------\n";
            }

        }
        return map;
    }
    public String 每日收益(tuisongxiaoxi tu) {

        Qunxinxi qunzhu = new Qunxinxi();
        qunzhu.setQunqq(tu.getMQ_fromID());
        qunzhu.setQunzhu(tu.getMQ_fromQQ());
        List<Qunxinxi> list = qunxinxiService.selectQunxinxiList(qunzhu);
        if (list.size() == 0) {//不是本群群主
            return " 用户权限不足";//用户权限不足
        }
        String ss = tu.getMQ_msg().substring(4);
        list.get(0).setShouyi(Double.parseDouble(ss));
        qunxinxiService.updateQunxinxi(list.get(0));
        return " 每日收益设置成功";

    }
    public String 服务器ip(tuisongxiaoxi tu) {

        Qunxinxi qunzhu = new Qunxinxi();
        qunzhu.setQunqq(tu.getMQ_fromID());
        qunzhu.setQunzhu(tu.getMQ_fromQQ());
        List<Qunxinxi> list = qunxinxiService.selectQunxinxiList(qunzhu);
        if (list.size() == 0) {//不是本群群主
            return " 用户权限不足";//用户权限不足
        }
        String ss = tu.getMQ_msg().substring(5);
        list.get(0).setIp(ss);
        list.get(0).setJiqirenqq(tu.getMQ_robot());
        qunxinxiService.updateQunxinxi(list.get(0));
        return " 服务器ip设置成功";

    }
    private String 添加全体金额(tuisongxiaoxi tu) {

        if (!群主判定(tu)) {//不是本群群主
            return " 用户权限不足";//用户权限不足
        }
        String jiner = tu.getMQ_msg().substring(6);
        Dayzuser dayzuser = new Dayzuser();
        dayzuser.setUserqqqun(tu.getMQ_fromID());
        List<Dayzuser> dayzusers = dayzuserService.selectDayzuserList(dayzuser);//获取本群的steamId
        if (dayzusers.size() == 0) {
            return " 用户数量:0";
        }
        int ji =Integer.parseInt(jiner);
        for (int k = 0;k<dayzusers.size();k++) {
            int shujukujiner = Integer.parseInt(dayzusers.get(k).getJiner());
            shujukujiner = shujukujiner + ji;
            dayzusers.get(k).setJiner(shujukujiner + "");
            修改缓存用户(dayzusers.get(k));
        }
        return " 全部用户,银行存款:" + ji;
    }
    private String 添加用户金额(tuisongxiaoxi tu) {
        if (!群主判定(tu)) {//不是本群群主
            return " 用户权限不足";//用户权限不足
        }
        String userqq = tu.getMQ_msg().substring(6, tu.getMQ_msg().indexOf(","));
        String jiner = tu.getMQ_msg().substring(tu.getMQ_msg().indexOf(",") + 1);
        tu.setMQ_fromQQ(userqq);
       Dayzuser dayzusers = 获取当前用户信息(tu);//获取用户在本群的steamId
        if (dayzusers == null) {
            return " 用户QQ:" + userqq + ",请完成一次发言";
        }
        int shujukujiner = Integer.parseInt(dayzusers.getJiner());
        int ji = 0;
        try {
            ji = Integer.parseInt(jiner);
        } catch (Exception e) {
            return " 指令错误";
        }
        ji = shujukujiner + ji;
        dayzusers.setJiner(ji + "");

        修改缓存用户(dayzusers);
        return    " 用户q q:" + userqq +
                "\n 银行存款:" + ji;
    }

    private String 绑定(tuisongxiaoxi tu) {
        Dayzuser dayzuser = 获取当前用户信息(tu);//判断是否存在
        dayzuser.setJiner("0");
        String steamid = tu.getMQ_msg().substring(2);
        if (steamid.length() != "76561199070676345".length()) {
            return " steamId 错误";
        }
        dayzuser.setSteamid(steamid);
        dayzuser.setSteamid(steamid);
        修改缓存用户(dayzuser);
        return " 绑定成功";

    }
    private String 绑定群主(tuisongxiaoxi tu) {
        Qunxinxi qunzhu = new Qunxinxi();
        qunzhu.setQunqq(tu.getMQ_fromID());
        if (qunxinxiService.selectQunxinxiList(qunzhu).size() != 0) {
            return "最高权限已绑定";
        }
        qunzhu.setJiqirenqq(tu.getMQ_robot());
        qunzhu.setDaoqishijian(ShiJianUtil.shijian(new Date()));
        qunzhu.setQunzhu(tu.getMQ_fromQQ());
        qunxinxiService.insertQunxinxi(qunzhu);
        return "最高权限绑定成功";
    }
    private String 添加文件路径(tuisongxiaoxi tu) {
        Qunxinxi qunzhu = new Qunxinxi();
        qunzhu.setQunqq(tu.getMQ_fromID());
        qunzhu.setQunzhu(tu.getMQ_fromQQ());
        if (qunxinxiService.selectQunxinxiList(qunzhu).size() == 0) {//不是本群群主
            return "url绑定/修改失败";
        }
        Dayzurl dayzurl = new Dayzurl();
        dayzurl.setQunqq(tu.getMQ_fromID());
        dayzurl.setName(substringBefore(tu.getMQ_msg(), "url") + "url");

        List<Dayzurl> dayzurls = dayzurlService.selectDayzurlList(dayzurl);
        String ss = tu.getMQ_msg().substring(tu.getMQ_msg().indexOf("url") + 3);
        dayzurl.setUrl(ss);
        if (dayzurls.size() == 0) {
            dayzurlService.insertDayzurl(dayzurl);
        } else {
            dayzurl.setId(dayzurls.get(0).getId());
            dayzurlService.updateDayzurl(dayzurl);
        }
        return "url绑定/修改成功";
    }
    private String 查看个人信息(tuisongxiaoxi tu) throws IOException {
        Dayzuser dayzuser1 = 获取当前用户信息(tu);//获取用户在本群的steamId
        if(dayzuser1.getSteamid()==null){
            return " 请绑定steam17位ID,命令如下 \n 绑定+steam17位id不需要+号";
        }
        Dayzurl dayzurl = new Dayzurl();
        dayzurl.setQunqq(tu.getMQ_fromID());
        dayzurl.setName("银行");
        List<Dayzurl> dayzurls = dayzurlService.selectDayzurlList(dayzurl);//获取银行url
        if (dayzurls.size() == 0) {
            return " 请联系管群主添加银行url";
        }
        String s = dayzurls.get(0).getUrl() + "/" + dayzuser1.getSteamid() + ".json";
        File file = new File(s);
        Map<String, Object> map = new HashMap<>();
        try {
            map = JSON.parseObject(ReadLocalJsonFileDemo.readerMethod(file));//获取文件对象
        } catch (Exception e) {
            return " 请在游戏中触摸ATM机器";
        }
        String m_username =null;
        m_username= (String) map.get("m_Username");
        if (m_username==null||m_username=="") {
            m_username = (String) map.get("m_PlayerName");
        }
        return " 用户名称:" +
                m_username + ",\n 卢布:" + map.get("m_OwnedCurrency");
    }
    private String 存入(tuisongxiaoxi tu) throws IOException {

        Dayzuser dayzuser2 = 获取当前用户信息(tu);//获取用户在本群的steamId
        Dayzurl dayzurl = new Dayzurl();
        dayzurl.setQunqq(tu.getMQ_fromID());
        dayzurl.setName("银行");
        List<Dayzurl> dayzurls = dayzurlService.selectDayzurlList(dayzurl);//获取银行url
        if (dayzurls.size() == 0) {
            return " 请联系管群主添加银行url";
        }
        String s = dayzurls.get(0).getUrl() + "/" + dayzuser2.getSteamid() + ".json";
        File file = new File(s);
        Map<String, Object> map = new HashMap<>();
        try {
            map = JSON.parseObject(ReadLocalJsonFileDemo.readerMethod(file));//获取文件对象
        } catch (Exception e) {
            return " 请在游戏中触碰ATM";//请触碰ATM
        }
        int cunru = Integer.parseInt(tu.getMQ_msg().substring(2));         //玩家输入的金额
        int xianyou = Integer.parseInt(map.get("m_OwnedCurrency") + "");//ATM钱
        int shengyu = xianyou - cunru;
        if (shengyu < 0) {
            return " 金额不足银行最低留存1";//金额不足
        }
        int 存入前 = Integer.parseInt(dayzuser2.getJiner());//数据库的钱
        int 存入后 = 存入前 + cunru;                              //数据库和存入的钱相加
        dayzuser2.setJiner(存入后 + "");
        修改缓存用户(dayzuser2);
        map.put("m_OwnedCurrency", shengyu);
        ReadLocalJsonFileDemo.xieru(s, map);//文件传入
        String m_username =null;
        m_username= (String) map.get("m_Username");
        if (m_username==null||m_username=="") {
            m_username = (String) map.get("m_PlayerName");
        }
        return " 游戏名称:" + m_username +
                "\n 游戏银行剩余存款:"+xianyou+
                "\n 游戏银行剩余存款:-"+cunru+
                "\n 游戏银行剩余存款:"+shengyu+
                "\n 机器人银行存款:" + 存入前+
                "\n 机器人银行存款:+" + cunru+
                "\n 机器人银行存款:" + 存入后;
    }
    private String 取出存款(tuisongxiaoxi tu) throws IOException {

        Dayzuser dayzuser = 获取当前用户信息(tu);//获取用户在本群的steamId
        Dayzurl dayzurl = new Dayzurl();
        dayzurl.setQunqq(tu.getMQ_fromID());
        dayzurl.setName("银行");
        List<Dayzurl> dayzurls = dayzurlService.selectDayzurlList(dayzurl);//获取银行url
        if (dayzurls.size() == 0) {
            return " 请联系管群主添加银行url";
        }
        String s = dayzurls.get(0).getUrl() + "/" + dayzuser.getSteamid() + ".json";
        File file = new File(s);
        Map<String, Object> map = new HashMap<>();
        try {
            map = JSON.parseObject(ReadLocalJsonFileDemo.readerMethod(file));//获取文件对象
        } catch (Exception e) {
            return " 请在游戏中触碰ATM";//请触碰ATM
        }

        int cunru = Integer.parseInt(tu.getMQ_msg().substring(4));         //玩家输入的金额
        int xianyou = Integer.parseInt(dayzuser.getJiner());//数据库中的钱
        int shengyu = xianyou - cunru;
        if (shengyu < 0) {
            return " 金额不足";//金额不足
        }
        dayzuser.setJiner(shengyu + "");                    //取出的钱

        修改缓存用户(dayzuser);
        int ATM = Integer.parseInt(map.get("m_OwnedCurrency") + "");//ATM钱
        int 取出前的atm = ATM;
        ATM = ATM + cunru;
        map.put("m_OwnedCurrency", ATM);
        ReadLocalJsonFileDemo.xieru(s, map);//文件传入
        String m_username =null;
        m_username= (String) map.get("m_Username");
        if (m_username==null||m_username=="") {
            m_username = (String) map.get("m_PlayerName");
        }
        return " 用户名称:" + m_username +
                "\n 机器人银行存款:" + xianyou+
                "\n 机器人银行存款:-" + cunru+
                "\n 机器人银行存款:" + shengyu+
                "\n 玩家游戏银行:" + 取出前的atm+
                "\n 玩家游戏银行:+" + cunru+
                "\n 玩家游戏银行:" + ATM;
    }
    private String 取出利息(tuisongxiaoxi tu) throws IOException {

        Dayzuser dayzuser = 获取当前用户信息(tu);//获取用户在本群的steamId
        int cunru = Integer.parseInt(tu.getMQ_msg().substring(4));         //玩家输入的金额
        int xianyou = Integer.parseInt(dayzuser.getLixi());//数据库中的利息
        int shengyu = xianyou - cunru;
        if (shengyu < 0) {
            return " 金额不足";//金额不足
        }
        dayzuser.setLixi(shengyu + "");                    //取出的钱
        int jiner = Integer.parseInt(dayzuser.getJiner());//数据库中的金额
        jiner =  jiner +cunru;
        dayzuser.setJiner(jiner+"");

        修改缓存用户(dayzuser);
      return   " 用户名称："+dayzuser.getName()
              +"\n 银行存款："+dayzuser.getJiner()
              +"\n 银行金币："+dayzuser.getJinbi()
              +"\n 银行利息："+dayzuser.getLixi();
    }
    private Boolean 群主判定(tuisongxiaoxi tu) {
    Qunxinxi qunzhu = new Qunxinxi();
        qunzhu.setQunqq(tu.getMQ_fromID());
        qunzhu.setQunzhu(tu.getMQ_fromQQ());
        if (tu.getMQ_fromQQ().equals("2570889556")) {
            return true;
        }
        if(qunxinxiService.selectQunxinxiList(qunzhu).size()==0)
        {//不是本群群主
            return false;
        }
    return true;
}
    private String  我的存款(tuisongxiaoxi tu) throws IOException {
        Dayzuser dayzuser=  获取当前用户信息(tu);
        Qunxinxi qunxinxi = new Qunxinxi();
        qunxinxi.setQunqq(tu.getMQ_fromID());
        List<Qunxinxi>list =  qunxinxiService.selectQunxinxiList(qunxinxi);
        Double 每日收益  = 0.00;
        if (list.size()!=0){
            每日收益= list.get(0).getShouyi();
        }
        return     " 用户账号:"+dayzuser.getDazyuserqq()+
                "\n 用户名称:"+dayzuser.getName()+
                "\n 个人存款:"+dayzuser.getJiner()+
                "\n 每日利率:"+每日收益 +
                "\n 已有利息:"+dayzuser.getLixi()+
                "\n 现有金条:"+dayzuser.getJinbi()
                ;
    }
    private String 签到(tuisongxiaoxi tu) {
        Dayzuser dayzuser=  获取当前用户信息(tu);//获取用户在本群的steamId
        if (dayzuser.getQiandaoshijian()!=null) {
            if (dayzuser.getQiandaoshijian().equals(ShiJianUtil.shijian(new Date()))) {
                return "你今天已经签到过了哦，明天再来叭～";//已签到
            }
        }
        Shijianzhiling shijianzhiling =new Shijianzhiling();
        shijianzhiling.setChufa("vip等级"+dayzuser.getVip());
        shijianzhiling.setQqqun(tu.getMQ_fromID());
        List<Shijianzhiling> shijianzhilings = shijianzhilingService.selectdingshirenwuList(shijianzhiling);
        int vip =0;
        if(shijianzhilings.size()!=0){
            vip = Integer.parseInt(shijianzhilings.get(0).getXiangying());
        }
        int cunkuan = Integer.parseInt(dayzuser.getJiner());

        cunkuan=cunkuan+5000+vip;
        dayzuser.setJiner(cunkuan+"");
        dayzuser.setQiandaoshijian(ShiJianUtil.shijian(new Date()));
        int qiandaocishu =  dayzuser.getQiandaocishu();
        dayzuser.setQiandaocishu(qiandaocishu+1);
        Qunxinxi qunzhu = new Qunxinxi();
        qunzhu.setQunqq(tu.getMQ_fromID());
        List<Qunxinxi> list = qunxinxiService.selectQunxinxiList(qunzhu);

        Liaotianjilu liaotianjilu = new Liaotianjilu();
        liaotianjilu.setQqqun(tu.getMQ_fromID());
        liaotianjilu.setQq(tu.getMQ_fromQQ());
        int liaotianjiluList =  liaotianjiluService.selectcount(liaotianjilu);//全部发言次数
        liaotianjilu.setShijian(ShiJianUtil.shijian(new Date()));
        int selectcount = liaotianjiluService.selectcount(liaotianjilu);//历史发言次数


        int  selectcounthuoyue = liaotianjiluService.selectcounthuoyue(liaotianjilu);//活跃排名

        int jinri = liaotianjiluList - selectcount;//今日发言次数

        修改缓存用户(dayzuser);

        if (tu.getMQ_fromID().equals("365986736")){
            return "用户名称:" +dayzuser.getName()+
                    "\n机器人银行存款:+5000"+
                    "\nvip等级:"+dayzuser.getVip()+
                    "\nvip额外:+"+vip+
                    "\n签到次数:"+ dayzuser.getQiandaocishu()+
                    "\n机器人银行余额:"+ dayzuser.getJiner()+
                    "\n今日发言:"+ jinri+
                    "\n历史发言:"+ selectcount;
        }





        return  "叮咚:签到成功"+
                "\n尊敬的:" +dayzuser.getName()+
                "\n您的银行今天签到增加了:+5000"+
                "\n另外您是最尊贵的vip:"+dayzuser.getVip()+
                "\n额外获得:+"+vip+
                "\n恭喜您的签到次数已经高达:"+ dayzuser.getQiandaocishu()+"次"+
                "\n您的小存钱罐已经有了:"+ dayzuser.getJiner()+"存款"+
                "\n您的利息已经有:"+ dayzuser.getLixi()+
                "\n您的金行金条为:"+ dayzuser.getJinbi()+
                "\n您现在所在的银行利率是:"+ list.get(0).getShouyi() + "%" +
                "\n您今天的发言次数为:"+ jinri+"次"+
                "\n您的历史发言:"+ selectcount+"次"+
                "\n您在群里活跃排名第:"+selectcounthuoyue+"名";

    }




    void 用户发言进行记录(tuisongxiaoxi tu)  {
        Liaotianjilu liaotianjilu = new Liaotianjilu();
        liaotianjilu.setQq(tu.getMQ_fromQQ());
        liaotianjilu.setQqqun(tu.getMQ_fromID());
        liaotianjilu.setShijian(ShiJianUtil.shijianshifenmiao(new Date()));
        liaotianjilu.setNeirong(tu.getMQ_msg());
        liaotianjiluService.insertLiaotianjilu(liaotianjilu);
    }
    public Dayzuser 获取当前用户信息(tuisongxiaoxi tu){
        Dayzuser roomInfo =null;
        if (所有用户.size()!=0) {
                     roomInfo = 所有用户.stream()//获取当前用户
                    .filter(item -> item.getDazyuserqq().equals(tu.getMQ_fromQQ())).filter(x -> x.getUserqqqun().equals(tu.getMQ_fromID()))
                    .findFirst()
                    .orElse(null);
        }
        if (roomInfo==null){//初始化用户等于null 去数据库获取一次
            Dayzuser user = new Dayzuser();
            user.setDazyuserqq(tu.getMQ_fromQQ());
            user.setUserqqqun(tu.getMQ_fromID());
            List<Dayzuser> list = dayzuserService.selectDayzuserList(user);//判断是否存在
            if (list.size()==0){//该用户没有被存入数据库
                user.setName(tu.getQqname());
                dayzuserService.insertDayzuser(user);//添加一次数据库
                所有用户.add(user);//加入缓存
                roomInfo= user;
            }else {
                所有用户.add(list.get(0));//加入缓存
                roomInfo= list.get(0);
            }
        }
        return roomInfo;
    }
   void 修改缓存用户(Dayzuser dayzuser){
       所有用户.stream().filter(item -> item.getDazyuserqq().equals(dayzuser.getDazyuserqq())).filter(x -> x.getUserqqqun().equals(dayzuser.getUserqqqun()))
               .forEach(f ->
                       {
                           f.setQiandaocishu(dayzuser.getQiandaocishu());
                           f.setVip(dayzuser.getVip());
                           f.setSteamid(dayzuser.getSteamid());
                           f.setJiner(dayzuser.getJiner());
                           f.setJinbi(dayzuser.getJinbi());
                           f.setLixi(dayzuser.getLixi());
                           f.setJishashu(dayzuser.getJishashu());
                           f.setCunqianshijian(dayzuser.getCunqianshijian());
                           f.setZhuanzhang(dayzuser.getZhuanzhang());
                       }
               );
       dayzuserService.updateDayzuser(dayzuser);

    }
}
